package com.mondo.financialwallet.presentation.ui.activity;

import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.common.ui.BaseActivity;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.presentation.event.LogOutEvent;
import com.mondo.financialwallet.presentation.ui.fragment.FinancialFragment;
import com.mondo.financialwallet.presentation.ui.fragment.FindFragment;
import com.mondo.financialwallet.presentation.ui.fragment.MyFragment;
import com.mondo.financialwallet.presentation.ui.fragment.WalletFragment;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;

import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import butterknife.Bind;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @Bind(R.id.bottom_navigation) RadioGroup mBottomNavigationView;

    private Map<Integer, Fragment> mFragmentMap = new HashMap<>();

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initActions() {
        // 添加Fragment
        mFragmentMap.put(R.id.nav_wallet, WalletFragment.newInstance());
        mFragmentMap.put(R.id.nav_financial, FinancialFragment.newInstance());
        mFragmentMap.put(R.id.nav_find, FindFragment.newInstance());
        mFragmentMap.put(R.id.nav_my, MyFragment.newInstance());

        mBottomNavigationView.setOnCheckedChangeListener(this);
        ((RadioButton)mBottomNavigationView.findViewById(R.id.nav_wallet)).setChecked(true);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        Fragment targetFragment = mFragmentMap.get(checkedId);

        if (targetFragment != null) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            if (!targetFragment.isAdded()) {
                fragmentTransaction.add(R.id.content, targetFragment);
            }

            Set<Map.Entry<Integer, Fragment>> entries = mFragmentMap.entrySet();
            for (Map.Entry<Integer, Fragment> fragmentEntry : entries) {
                Fragment fragmentEntryValue = fragmentEntry.getValue();
                if (fragmentEntryValue == targetFragment) {
                    fragmentTransaction.show(fragmentEntryValue);
                } else {
                    fragmentTransaction.hide(fragmentEntryValue);
                }
            }

            fragmentTransaction.commit();
        }
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // super.onSaveInstanceState(outState);
        // 解决系统回收Activity，Fragment叠加问题
    }

    @Subscribe
    public void logoutEvent(LogOutEvent event){
        finish();
    }
}
